package org.jdesktop.swingx.input;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;

import vnreal.ZNEW.GraphTest.Node;
import vnreal.ZNEW.GraphTest.SubstrateLink;
import vnreal.ZNEW.GraphTest.WorkOnPaths;
import vnreal.gui.menu.MapMenu;
import vnreal.map.FancyWaypointRenderer;
import vnreal.map.Map;
import vnreal.map.MyWaypoint;

/**
 * Centers the map on the mouse cursor if Right is double-clicked or middle
 * mouse button is pressed.
 * 
 * @author Martin Steiger
 * @author joshy
 */
public class Nodes {
	static JDialog dialog;
	private JXMapViewer viewer;

	public Nodes(JXMapViewer viewer) {
		this.viewer = viewer;
		openDialog(viewer);
	}

	private ActionListener okClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			Map.drawAll();
			dialog.dispose();
			MapMenu.frame.setEnabled(true);
		}
	};
	public static String selected;
	private ActionListener editClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			if(nodes.getItemCount() > 0) {
				selected = (String) nodes.getSelectedItem();
				dialog.setEnabled(false);
				new EditNode(viewer);
			}
		}
	};
	private ActionListener removeClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			if(nodes.getItemCount() > 0) {
				int i = 0;
				boolean isLeft = true;
				while(isLeft) {
					i = 0;
					isLeft = false;
					for(SubstrateLink link : WorkOnPaths.substrateEdges) {
						if(link.src != null) {
							if(link.src.globalNodeId.equals(nodes.getSelectedItem())) {
								isLeft = true;
								break;
							}
						}
						++i;
					}
					if(isLeft) {
						WorkOnPaths.substrateEdges.remove(i);
					}
				}
				isLeft = true;
				while(isLeft) {
					i = 0;
					isLeft = false;
					for(SubstrateLink link : WorkOnPaths.substrateEdges) {
						if(link.dst != null) {
							if(link.dst.globalNodeId.equals(nodes.getSelectedItem())) {
								isLeft = true;
								break;
							}
						}
						++i;
					}
					if(isLeft) {
						WorkOnPaths.substrateEdges.remove(i);
					}
				}
				for(Node node : WorkOnPaths.substrateNodes) {
					if(node.globalNodeId.equals(nodes.getSelectedItem())) {
						WorkOnPaths.substrateNodes.remove(node);
						break;
					}
				}
				i = nodes.getSelectedIndex();
				comboBoxItems.remove(i);
				if(i > 0) {
					nodes.setSelectedIndex(--i);
				}
				else if (i == 0) {
					if(!comboBoxItems.isEmpty()) {
						nodes.setSelectedIndex(i);
					}
				}
			}
		}
	};
	private JComboBox nodes;
	static Vector<String> comboBoxItems;
	private void openDialog(JXMapViewer viewer) {
		dialog = new JDialog(MapMenu.frame, "Nodes");
		JLabel label;
		dialog.setLayout(new GridLayout(6,2));
		dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		label = new JLabel("   Nodes");
		dialog.add(label);

		comboBoxItems = new Vector<String>();
		for (Node node : WorkOnPaths.substrateNodes) {
			comboBoxItems.add(node.globalNodeId);
		}
		final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(
				comboBoxItems);
		nodes = new JComboBox(model);
		dialog.add(nodes);

		dialog.add(new JLabel());
		
		dialog.add(new JLabel());
		
		JButton editButton = new JButton("Edit");
		editButton.addActionListener(editClicked);
		dialog.add(editButton);
		
		dialog.add(new JLabel());
		
		JButton removeButton = new JButton("Remove");
		removeButton.addActionListener(removeClicked);
		dialog.add(removeButton);
		
		dialog.add(new JLabel());
		
		dialog.add(new JLabel());
		dialog.add(new JLabel());
		dialog.add(new JLabel());

		JButton okButton = new JButton("OK");
		okButton.addActionListener(okClicked);
		dialog.add(okButton);

		dialog.setSize(500, 200);
		dialog.setResizable(false);
		// dialog.setAlwaysOnTop(true);
		dialog.setVisible(true);
	}
}
